package com.cloudoer.blog.controller;

import com.cloudoer.blog.base.BaseController;
import com.cloudoer.blog.model.Article;
import com.cloudoer.blog.model.info.ArticleInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping(value = "/article")
public class ArticleController extends BaseController {

    @RequestMapping(value = "/index")
    public String index (){
        return "article/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add (ModelMap modelMap){
        modelMap.put("action", "/article/update");
        modelMap.put("tags", tagService.getAll());
        ArticleInfo article = articleService.add(new Article());
        modelMap.put("article", article);
        return "article/add";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update (String id, ModelMap modelMap){
        modelMap.put("action", "/article/update");
        modelMap.put("tags", tagService.getAll());
        modelMap.put("article", articleService.getById(id));
        return "article/add";
    }

    @RequestMapping(value = "/getById")
    @ResponseBody
    public ArticleInfo getById(String id){
        return articleService.getById(id);
    }

    @RequestMapping(value = "/getByPage")
    @ResponseBody
    public Map getByPage (@RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<ArticleInfo> list = articleService.getByPage((pno-1)*psize, psize);
        return result(list, articleService.getCount(), "");
    }

    @RequestMapping(value = "/getByPagePub")
    @ResponseBody
    public Map getByPagePub (@RequestParam(value = "pno", defaultValue = "1") Integer pno, @RequestParam(value = "psize", defaultValue = "10") Integer psize){
        List<ArticleInfo> list = articleService.getByPagePub((pno-1)*psize, psize);
        return result(list, articleService.getCountByPub(), "");
    }

    @RequestMapping(value = "/getByRan")
    @ResponseBody
    public Map getByPageRan (){
        List<ArticleInfo> list = articleService.getByRan();
        return result(list, 0, "");
    }

    @RequestMapping(value = "/getByEnd")
    @ResponseBody
    public Map getByEnd (){
        List<ArticleInfo> list = articleService.getByEnd();
        return result(list, 0, "");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Map update (Article article, String tags){
        articleService.update(article, tags);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    @ResponseBody
    public Map publish (String id){
        articleService.publish(id);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map delete (String id){
        articleService.delete(id);
        fileUtil.deleteImg(id);
        return result(SUCCESS, "操作成功");
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Map upload (String id, MultipartFile img){
        Map<String, Object> map = new HashMap();
        if (articleService.getById(id) == null){
            map.put("errno", "1");
            map.put("data", "文章不存在");
        }else{
            try {
                String url = fileUtil.uploadFile(img, id, UUID.randomUUID().toString().replace("-", ""));
                map.put("errno", 0);
                map.put("data", new String[]{url});
            } catch (ServletException e) {
                e.printStackTrace();
                map.put("errno", "1");
                map.put("data", e.getMessage());
            } catch (IOException e) {
                e.printStackTrace();
                map.put("errno", "1");
                map.put("data", "未知错误");
            }finally {
                return map;
            }
        }
        return map;
    }

}
